/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TancamentProves.java
 *
 * Created on 04-may-2012, 10:32:12
 */
package client.subsistema2;

import beans.Prova;
import beans.Ronda;
import beans.RondaDTO;
import beans.Constants;
import common.EOlympicsException;
import common.RemoteInterface;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Daniel
 */
public class TancamentProves extends javax.swing.JFrame {

    /** Creates new form TancamentProves */
    public TancamentProves(RemoteInterface pRemoto, int usuari) {
        initComponents();
        codiUsuari=usuari;
        remoto=pRemoto;

        // quan sintegri llevar això i comentar al main()
        /*
        try {
            remoto=(RemoteInterface)Naming.lookup(url);
        } catch (NotBoundException ex) {
            Logger.getLogger(AltaProves.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(AltaProves.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(AltaProves.class.getName()).log(Level.SEVERE, null, ex);
        }
        */
        // fi de llevar
        
    }

    int codiUsuari;
    
    java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("i18n/subsistema2/messagesSubsistema2"); // NOI18N
    RemoteInterface remoto;
    
    /* quan no hio ha main llevar-ho */    
    private final String url = new String("rmi://localhost/RemoteInterface");
    /* fi de llevar */

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jToolBar1 = new javax.swing.JToolBar();
        jButton7 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("eOlympics - Tancament de proves");

        jButton1.setText("Cercar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Tancar Competició");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Prova", "Tipus", "Estat"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jToolBar1.setRollover(true);

        jButton7.setText("Cercar");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton7);

        jButton3.setText("Tancar competició");
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton3);

        jMenu1.setText("Arxiu");

        jMenuItem1.setText("Cercar");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Tancar competició");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuItem3.setText("Sortir");
        jMenu1.add(jMenuItem3);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jButton1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton2))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 564, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    List<RondaDTO> lista;
    
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            lista=remoto.sub2cercarCompteticionsActives();
                       jTable1.removeAll();

               DefaultTableModel model = new DefaultTableModel();

               model.addColumn("Prova");
               model.addColumn("Tipus ronda");
               model.addColumn("Estat");        

               for(RondaDTO rdto: lista)
               {
                   String[] cols = {rdto.getDescripcio(),rdto.getRonda().getTipusRonda(),"En competició"};
                   model.addRow(cols);                
               }
               jTable1.setModel(model);
        } catch (RemoteException ex) {
            System.out.println("Problemes cercant competicions");
        }catch (EOlympicsException e) {
               System.out.print("EOlympicsException");
               System.out.println(bundle.getString("client.carregamt_competicions"));
       }

}//GEN-LAST:event_jButton1ActionPerformed
/* tancar la prova seleccionada */
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
   System.out.println("pasa "+jTable1.getSelectedRow());
    //System.out.print(jFormattedTextField1.getValue().toString());
    if (jTable1.getSelectedRow()!=-1)
    {
            try {
                Ronda retorn=lista.get(jTable1.getSelectedRow()).getRonda();
                remoto.sub2tancaCompeticio(retorn);
                // canvi d'estat a la prova
                System.out.println("Ronda tancada "+retorn.getTipusRonda());
                Prova prova=new Prova();
                prova.setIdProva(retorn.getIdProva());
                    try {
                        prova=remoto.sub2carregaProva(prova);
                        if(prova.getEstat().equals(Constants.PROVA_EN_COMPETICIO_PREVIA))
                            prova.setEstat(Constants.PROVA_EN_CLASSIFICACIO_PREVIA);
                        else
                            prova.setEstat(Constants.PROVA_EN_CLASSIFICACIO_FINAL);
                        System.out.println("pos "+prova.getEstat());
                        prova.setUsuariMod(new Integer(codiUsuari));
                        remoto.sub2modifica(prova);
                    } catch (RemoteException ex) {
                        System.out.println("Errada modificant la prova");
                    }
                jButton1ActionPerformed(evt);
            } catch (RemoteException ex) {
                System.out.println("Problemes tancant la comptetició");
            }catch (EOlympicsException e) {
               System.out.print("EOlympicsException");
               System.out.println(bundle.getString("client.problemes_tancant_competicio"));
       }
    }
    
}//GEN-LAST:event_jButton2ActionPerformed

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed

    jButton1ActionPerformed(evt);
}//GEN-LAST:event_jMenuItem1ActionPerformed

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed

    jButton2ActionPerformed(evt);
}//GEN-LAST:event_jMenuItem2ActionPerformed

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed

    jButton1ActionPerformed(evt);
}//GEN-LAST:event_jButton7ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

    jButton2ActionPerformed(evt);
}//GEN-LAST:event_jButton3ActionPerformed

    /*
    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TancamentProves.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TancamentProves.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TancamentProves.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TancamentProves.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TancamentProves(2).setVisible(true);
            }
        });
    }
    */


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton7;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables
}
